package hutool.word;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcelFactory;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;


import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/5/26 9:50
 * @since JDK 1.8
 */
public class Word {



    //外链过滤
    public static void main(String[] args) {
        excelRead2();
        //String str=readWord("C:\\Users\\xiong\\Desktop\\测试docx文档.docx");
        //System.out.println(str);
       // String str="<span><a href='/info_143_itemid_25713.html' target=\"_blank\"><img src=\"/uploadfiles/2019/02/201902261634423442.jpg\" onerror=\"this.src='/res/images/nofile.jpg'\"/></a></span>";
        //String str1="<img id=\"14620807\" title=\"在广州做公务员的香港青年黄钊逸。刘冠南 摄\" alt=\"在广州做公务员的香港青年黄钊逸。刘冠南 摄\" src=\"http://www.gdzz.gov.cn/img/0/8/8835/11647.jpg\" width=\"604\" height=\"805\" srcset=\"\" class=\"nfw-cms-img\" img-id=\"8835\" data-catchresult=\"img_catchSuccess\" style=\"padding:0px;margin:10px auto;border-width:initial;border-style:none;vertical-align:middle;display:block;max-width:100%;\" /></p><p style=\"padding:0px;margin:30px;border:none;color:#333333;font-family:sans-serif, &quot;font-size:20px;white-space:normal;background-color:#FFFFFF;\">";
        //hrefFilter(str1);
        //wordRead();
    }


    public static void excelRead2(){

        ExcelReader reader = ExcelUtil.getReader("C:\\Users\\xiong\\Desktop\\新建 XLS 工作表.xls");
        List<List<Object>> readAll = reader.read();
        readAll.forEach(e->{
            System.out.println(e);
        });

//        FileInputStream fileInputStream = null;
//        try {
//            fileInputStream = new FileInputStream("C:\\Users\\xiong\\Desktop\\新建 XLSX 工作表 (2).xlsx");
//            StringExcelListener listener = new StringExcelListener();
//            ExcelReader excelReader = EasyExcelFactory.read(fileInputStream, null, listener).headRowNumber(0).build();
//            excelReader.read();
//            List<List<String>> datas = listener.getDatas();
//            excelReader.finish();
//            datas.forEach(e->{
//                System.out.println(e);
//            });
//        }catch (Exception e){
//            e.printStackTrace();
//        }


    }



    public static void excelRead(){
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("C:\\Users\\xiong\\Desktop\\新建 XLSX 工作表 (2).xlsx");
            XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
            //获取sheet
            int num = sheets.getActiveSheetIndex();  //获取sheets的数量
            getSheetList(num,sheets);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public static void getSheetList(int num,XSSFWorkbook sheets){
        for (int i = 0; i <= num ; i++) {
            XSSFSheet sheet=sheets.getSheetAt(i);
            int rows=sheet.getPhysicalNumberOfRows();
            System.out.println("sheet"+i+"总计 : "+rows+"行");
            for(int m=0;m<=rows;m++){
                //获取列数
                XSSFRow row = sheet.getRow(m);
                if(row==null){
                    System.out.println(m+"行为空");
                    continue;
                }
                int columns = row.getPhysicalNumberOfCells();
                System.out.println(columns+"列");
                for(int j=0;j<=columns;j++){
                    if(row.getCell(j)==null){
                        System.out.println("\n"+j+"列为空");
                        continue;
                    }
                    String cell = row.getCell(j).toString();
                    System.out.print(cell+"  ");
                }
                System.out.println();
            }
            System.out.println("--------------------------------");
        }
    }

//    //打印excel数据
//    public static void readExcelData(){
//        //获取行数
//        int rows = sheet.getPhysicalNumberOfRows();
//        for(int i=0;i<rows;i++){
//            //获取列数
//            XSSFRow row = sheet.getRow(i);
//            int columns = row.getPhysicalNumberOfCells();
//            for(int j=0;j<columns;j++){
//                String cell = row.getCell(j).toString();
//                System.out.println(cell);
//            }
//        }
//    }



    public static void wordRead(){
        String path="C:\\Users\\xiong\\Desktop\\新建 DOC 文档.doc";
//        String str=readWord(path);
        //System.out.println(str);
        File file=new File("C:\\Users\\xiong\\Desktop\\新建 DOC 文档.doc");
    }


    /**
     * 读取word文件内容
     *
     * @param
     * @return buffer
     */
    public static String readWord(String path) {
        String buffer = "";
        FileInputStream is=null;
        OPCPackage opcPackage=null;
        try {
            if (path.endsWith(".doc")) {
                is = new FileInputStream(path);
                WordExtractor ex = new WordExtractor(is);
                buffer = ex.getText();
               // is.close();
            } else if (path.endsWith(".docx")) {
                opcPackage= POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                buffer = extractor.getText();
               // opcPackage.close();
            } else {
                System.out.println("此文件不是word文件！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(is!=null){
                    is.close();
                }
                if(opcPackage!=null){
                    opcPackage.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return buffer;
    }


   //过滤外链
    public static void hrefFilter(String htmlString){
        String regex="href=[\"'](http.*?)[\"']";  //过滤外链跳转
        String regex2="[\"'](http.*?)[\"']";   //过滤外链
        String regex3="[\"'](www.*?)[\"']";   //过滤www
        regexUtils(htmlString,regex3);
    }

    public static String regexUtils(String str,String regex){
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        StringBuffer stringBuffer=new StringBuffer();
        while(m.find()){
            System.out.println(m.group());
            stringBuffer.append(m.group()+"\n");
        }
        return stringBuffer.toString();
    }
}
